﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comissoes.Models;
using Comissoes.JQGridObject;
using Comissoes.Utilities;

namespace Comissoes.Controllers
{
    public class VolumesController : AppController
    {
        xcontEntities db = new xcontEntities();

        // Monta a lista de tipos de produtos
        public JsonResult List(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper)
        {
            try
            {
                var q = from c in db.VOLUMES
                        select new
                        {
                            c.ID_VOL,
                            c.ESPECIE_VOL,
                            c.MARCA_VOL,
                            c.NUMERACAO_VOL,
                            c.QTDE_VOL,
                            c.PESOBRT_VOL,
                            c.PESOLIQ_VOL
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_VOL", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Edit(Int32? id)
        {
            VolumesModel volume = new VolumesModel(id);

            return View(volume);
        }

        [HttpPost]
        public ActionResult Edit(FormCollection form)
        {
            List<String> erros = new List<string>();
            VolumesModel volume = new VolumesModel(Convertions.Inteiro(form["ID_VOL"]));

            erros = volume.UpdateValidate(form);

            if (!erros.Any())
            {
                erros.Union(volume.Update(form));
            }

            if (!erros.Any())
            {
                return Json(new { result = true, ID_VOL = volume.volume.ID_VOL }, JsonRequestBehavior.AllowGet);
            }

            return Json(new { result = false, erros = erros }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Delete(Int32 id)
        {
            List<string> erros = new List<string>();
            try
            {
                db.DeleteObject(db.VOLUMES.FirstOrDefault(t => t.ID_VOL == id));
                db.SaveChanges();

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                erros.Add(ex.Message);
                if (ex.InnerException != null)
                    erros.Add(ex.InnerException.Message);
            }

            return Json(erros, JsonRequestBehavior.AllowGet);
        }

        // Monta a lista de tipos de produtos
        public JsonResult LacresList(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper, Int32 ID_VOL)
        {
            try
            {
                var q = from c in db.LACRES
                        where c.VOLUMES.ID_VOL == ID_VOL
                        select new
                        {
                            act = "",
                            c.ID_LACRE,
                            c.NUMERO_LACRE
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q, page, sidx, sord, searchField, searchString, searchOper, "ID_LACRE", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }
        
        public ActionResult LacreUpdate(Int32? ID_VOL)
        {
            List<string> erros = new List<string>();

            try
            {
                VolumesModel volume = new VolumesModel(ID_VOL);
                erros.Union(volume.LacreUpdate(Request));

                if (!erros.Any())
                {
                    return Json(new { result = true }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                erros.Add(ex.Message);
                if (ex.InnerException != null)
                    erros.Add(ex.InnerException.Message);
            }

            return Json(new { result = false, erros = erros }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult LacreDelete(Int32 id)
        {
            List<string> erros = new List<string>();
            try
            {
                VolumesModel volume = new VolumesModel();
                volume.LacreDelete(id);

                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                erros.Add(ex.Message);
                if (ex.InnerException != null)
                    erros.Add(ex.InnerException.Message);
            }

            return Json(erros, JsonRequestBehavior.AllowGet);
        }
    }
}
